﻿@*用户编辑页面*@

@{
    Layout = "_Layout";
    string id = (string)ViewData["id"];

    string departmentId = (string)ViewData["departmentId"];
}
<div style="margin: 15px;">
    <form class="layui-form" id="updateForm" action="@Url.Action("Edit","User")">
        <div class="layui-form-item">
            <label class="layui-form-label">登录名</label>
            <div class="layui-input-block">
                <input type="text" name="userName" lay-verify="required" class="layui-input" data-bind="disabled:userName,value:userName">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">中文名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" data-bind="value: name" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" class="layui-input" data-bind="value:eMail" value="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-block">
                <input type="text" name="mobileNumber" data-bind="value:mobileNumber" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-block">
                <select name="departmentId" xm-select-radio=""  xm-select="selectdepartment" xm-select-height="50px" lay-verify="required">
                </select> 
            </div>
        </div>
          
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select name="userRoles" lay-verify="required" xm-select="selectRole" xm-select-show-count="4" xm-select-height="50px"></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remarks" class="layui-textarea" data-bind="text:remarks"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="btnSubmit">提交</button>
            </div>
        </div>
    </form>
    <script type="text/javascript">
        function vModel() {
            this.id = ko.observable("");
            this.userName = ko.observable("");
            this.name = ko.observable("");
            this.departmentId = ko.observable("");
            this.eMail = ko.observable("");
            this.loginTimes = ko.observable("");
            this.mobileNumber = ko.observable("");
            this.remarks = ko.observable("");
            this.userRoles = ko.observable("");
            this.roles = ko.observable("");
        }
        var model = new vModel();
        layui.use(['element', 'layer','jquery','form','formSelects'], function () {
            var $ = layui.jquery,
                form = layui.form,
                formSelects = layui.formSelects;
            $.ajax({
                type: "Get",
                url: "/User/Get?id=" + '@id' +"&departmentId=@departmentId", //获取数据的ajax请求地址
                success: function (res) {
                    if (res.success) {
                        ko.mapping.fromJS(res.data, {}, model);
                        //角色下拉
                        formSelects.data('selectRole', 'server', {
                            url: '/role/GetRoleSelectData',
                            success: function (id) {
                                var userRoles = model.userRoles();
                                var roles = [];
                                for (var i in userRoles) {
                                    roles.push(userRoles[i].roleId());
                                }
                                formSelects.value('selectRole', roles);
                            }
                        });
                        //部门下拉树
                        formSelects.data('selectdepartment', 'server', {
                            url: '/department/GetSelectData',
                            success: function (id) {
                                formSelects.value('selectdepartment', [model.departmentId()]);
                            }
                        });
                        form.render();
                    } else {
                        admin_error(res.msg);
                    }
                }
            });
           
            
            //监听提交
            form.on('submit(btnSubmit)', function (data) {
                model.roles(layui.formSelects.value('selectRole', 'valStr'));
                model.departmentId(layui.formSelects.value('selectdepartment', 'valStr'));
                $.ajax({
                    url: $("#updateForm").attr("action"),
                    type: 'post',
                    dataType: 'json',
                    async: false,
                    data: model,//$("#updateForm").serialize(),
                    success: function (res) {
                        if (res.success) {
                            admin_success();
                            parent.layer.closeAll('iframe');
                        } else {
                            admin_error(res.msg);
                        }
                    }
                });
                return false;
            });
        });
        ko.applyBindings(model);//注册vModel
    </script>
</div>